<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>洗牌算法 - Fisher-Yates 随机化技术</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-shadow {
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        .card-shadow:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            color: #d4d4d4;
            line-height: 1.6;
        }
        .keyword { color: #c586c0; }
        .function { color: #dcdcaa; }
        .variable { color: #9cdcfe; }
        .number { color: #b5cea8; }
        .string { color: #ce9178; }
        .comment { color: #6a9955; }
        .operator { color: #d4d4d4; }
        .bracket { color: #ffd700; }
        .shuffle-animation {
            animation: shuffle 2s ease-in-out infinite;
        }
        @keyframes shuffle {
            0%, 100% { transform: rotate(0deg); }
            25% { transform: rotate(-5deg); }
            75% { transform: rotate(5deg); }
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .feature-icon {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="text-center">
                <div class="mb-6">
                    <i class="fas fa-random text-6xl opacity-80 shuffle-animation"></i>
                </div>
                <h1 class="text-5xl font-bold mb-4" style="font-family: 'Noto Serif SC', serif;">
                    洗牌算法
                </h1>
                <p class="text-xl opacity-90 max-w-2xl mx-auto">
                    探索 Fisher-Yates 算法的优雅实现，掌握等概率随机打乱的艺术
                </p>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12">
        <!-- Problem Description Card -->
        <div class="bg-white rounded-2xl card-shadow p-8 mb-8">
            <div class="flex items-center mb-6">
                <i class="fas fa-question-circle text-3xl feature-icon mr-4"></i>
                <h2 class="text-3xl font-bold">题目描述</h2>
            </div>
            <p class="text-lg text-gray-700 leading-relaxed">
                请实现一种算法，将一组数据（如数组或牌组）进行等概率随机打乱，保证每种排列出现的概率相同。
            </p>
        </div>

        <!-- Core Concepts Grid -->
        <div class="grid md:grid-cols-2 gap-8 mb-8">
            <!-- Algorithm Card -->
            <div class="bg-white rounded-2xl card-shadow p-8">
                <div class="flex items-center mb-4">
                    <i class="fas fa-brain text-2xl feature-icon mr-3"></i>
                    <h3 class="text-2xl font-semibold">核心算法</h3>
                </div>
                <div class="space-y-3">
                    <div class="flex items-center">
                        <i class="fas fa-check-circle text-purple-600 mr-3"></i>
                        <span class="text-gray-700">Fisher-Yates 洗牌算法</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-check-circle text-purple-600 mr-3"></i>
                        <span class="text-gray-700">随机数生成技术</span>
                    </div>
                </div>
            </div>

            <!-- Complexity Card -->
            <div class="bg-white rounded-2xl card-shadow p-8">
                <div class="flex items-center mb-4">
                    <i class="fas fa-chart-line text-2xl feature-icon mr-3"></i>
                    <h3 class="text-2xl font-semibold">复杂度分析</h3>
                </div>
                <div class="space-y-3">
                    <div class="flex items-center">
                        <i class="fas fa-clock text-green-600 mr-3"></i>
                        <span class="text-gray-700">时间复杂度：O(n)</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-memory text-blue-600 mr-3"></i>
                        <span class="text-gray-700">空间复杂度：O(1)</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Solution Approach -->
        <div class="bg-white rounded-2xl card-shadow p-8 mb-8">
            <div class="flex items-center mb-6">
                <i class="fas fa-lightbulb text-3xl feature-icon mr-4"></i>
                <h2 class="text-3xl font-bold">解题思路</h2>
            </div>
            <div class="prose max-w-none">
                <p class="text-lg text-gray-700 mb-4">
                    Fisher-Yates 洗牌算法的核心思想是从后往前遍历数组，每次随机选择一个未处理的元素与当前位置交换。
                </p>
                <div class="bg-purple-50 border-l-4 border-purple-600 p-4 rounded-r-lg">
                    <p class="text-gray-700">
                        <strong>关键步骤：</strong>从数组末尾开始，每次随机选择一个索引范围在 [0, i] 的元素，与位置 i 的元素交换。
                    </p>
                </div>
            </div>
        </div>

        <!-- Algorithm Visualization -->
        <div class="bg-white rounded-2xl card-shadow p-8 mb-8">
            <div class="flex items-center mb-6">
                <i class="fas fa-project-diagram text-3xl feature-icon mr-4"></i>
                <h2 class="text-3xl font-bold">算法流程图</h2>
            </div>
            <div class="mermaid">
                graph TD
                    A[开始] --> B[i = 数组长度 - 1]
                    B --> C{i > 0?}
                    C -->|是| D[生成随机数 j ∈ [0, i]]
                    D --> E[交换 arr[i] 和 arr[j]]
                    E --> F[i = i - 1]
                    F --> C
                    C -->|否| G[洗牌完成]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style G fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#f3f4f6,stroke:#667eea,stroke-width:2px
                    style E fill:#f3f4f6,stroke:#667eea,stroke-width:2px
            </div>
        </div>

        <!-- Code Implementation -->
        <div class="bg-white rounded-2xl card-shadow p-8 mb-8">
            <div class="flex items-center mb-6">
                <i class="fas fa-code text-3xl feature-icon mr-4"></i>
                <h2 class="text-3xl font-bold">示例代码</h2>
            </div>
            <div class="code-block">
                <div class="code-header">
                    <div class="code-dot" style="background: #ff5f56;"></div>
                    <div class="code-dot" style="background: #ffbd2e;"></div>
                    <div class="code-dot" style="background: #27c93f;"></div>
                    <span class="text-gray-400 ml-4 text-sm">TypeScript</span>
                </div>
                <pre><code><span class="keyword">function</span> <span class="function">tsShuffle</span><span class="bracket">(</span><span class="variable">tsArr</span><span class="operator">:</span> <span class="keyword">number</span><span class="bracket">[]</span><span class="bracket">)</span><span class="operator">:</span> <span class="keyword">void</span> <span class="bracket">{</span>
  <span class="keyword">for</span> <span class="bracket">(</span><span class="keyword">let</span> <span class="variable">i</span> <span class="operator">=</span> <span class="variable">tsArr</span><span class="operator">.</span><span class="variable">length</span> <span class="operator">-</span> <span class="number">1</span><span class="operator">;</span> <span class="variable">i</span> <span class="operator">&gt;</span> <span class="number">0</span><span class="operator">;</span> <span class="variable">i</span><span class="operator">--</span><span class="bracket">)</span> <span class="bracket">{</span>
    <span class="keyword">const</span> <span class="variable">j</span> <span class="operator">=</span> <span class="variable">Math</span><span class="operator">.</span><span class="function">floor</span><span class="bracket">(</span><span class="variable">Math</span><span class="operator">.</span><span class="function">random</span><span class="bracket">()</span> <span class="operator">*</span> <span class="bracket">(</span><span class="variable">i</span> <span class="operator">+</span> <span class="number">1</span><span class="bracket">)</span><span class="bracket">)</span><span class="operator">;</span>
    <span class="bracket">[</span><span class="variable">tsArr</span><span class="bracket">[</span><span class="variable">i</span><span class="bracket">]</span><span class="operator">,</span> <span class="variable">tsArr</span><span class="bracket">[</span><span class="variable">j</span><span class="bracket">]</span><span class="bracket">]</span> <span class="operator">=</span> <span class="bracket">[</span><span class="variable">tsArr</span><span class="bracket">[</span><span class="variable">j</span><span class="bracket">]</span><span class="operator">,</span> <span class="variable">tsArr</span><span class="bracket">[</span><span class="variable">i</span><span class="bracket">]</span><span class="bracket">]</span><span class="operator">;</span>
  <span class="bracket">}</span>
<span class="bracket">}</span></code></pre>
            </div>
        </div>

        <!-- Key Points -->
        <div class="bg-gradient-to-r from-purple-600 to-purple-800 rounded-2xl p-8 text-white">
            <div class="flex items-center mb-6">
                <i class="fas fa-star text-3xl mr-4"></i>
                <h2 class="text-3xl font-bold">算法要点</h2>
            </div>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="text-center">
                    <i class="fas fa-dice text-4xl mb-3 opacity-80"></i>
                    <h3 class="text-xl font-semibold mb-2">等概率保证</h3>
                    <p class="opacity-90">每个元素在每个位置出现的概率均为 1/n</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-exchange-alt text-4xl mb-3 opacity-80"></i>
                    <h3 class="text-xl font-semibold mb-2">原地交换</h3>
                    <p class="opacity-90">不需要额外空间，直接在原数组上操作</p>
                </div>
                <div class="text-center">
                    <i class="fas fa-tachometer-alt text-4xl mb-3 opacity-80"></i>
                    <h3 class="text-xl font-semibold mb-2">线性时间</h3>
                    <p class="opacity-90">只需遍历一次数组，效率极高</p>
                </div>
            </div>
        </div>
    </main>

    <script>
        mermaid.initialize({ 
            startOnLoad: true,
            theme: '